/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Admin_EditStore.java
 *
 * Created on Mar 30, 2011, 11:47:16 PM
 */
package admin_gui;

import DTO.Address;
import DTO.Store;
import admin.BusinessLogic.Validation;
import admin.Controller.StoreController;
import java.util.Vector;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;

/**
 *
 * @author edaype
 */
public class Admin_EditStore extends javax.swing.JPanel {

    /** Creates new form Admin_EditStore */
    private StoreController storeController;
    private Vector edit_vect;
    private int aid = 0;

    public Admin_EditStore(StoreController aThis, Vector vector) {
        this.storeController = aThis;
        initComponents();
        edit_vect = ((Vector) vector.elementAt(0));
        StoreField.setText((String) edit_vect.get(0));
        StNumberField.setText((String) edit_vect.get(1).toString());
        StreetField.setText((String) edit_vect.get(2));
        CityFIeld.setText((String) edit_vect.get(3));
        PostalField.setText((String) edit_vect.get(4));
        ProvieceField.setText((String) edit_vect.get(5));
        PhoneField.setText((String) edit_vect.get(6));
        aid = Integer.parseInt(edit_vect.get(8).toString());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        ItemPanel = new javax.swing.JPanel();
        StoreLabel = new javax.swing.JLabel();
        PostalLabel = new javax.swing.JLabel();
        StNumberLabel = new javax.swing.JLabel();
        ProvinceLabel = new javax.swing.JLabel();
        StreetLabel = new javax.swing.JLabel();
        PhoneLabel = new javax.swing.JLabel();
        CityLabel = new javax.swing.JLabel();
        CityFIeld = new javax.swing.JTextField();
        StreetField = new javax.swing.JTextField();
        StNumberField = new javax.swing.JTextField();
        StoreField = new javax.swing.JTextField();
        PostalField = new javax.swing.JTextField();
        PhoneField = new javax.swing.JTextField();
        ProvieceField = new javax.swing.JTextField();
        ButtonPanel = new javax.swing.JPanel();
        ResetButton = new javax.swing.JButton();
        SubmitButton = new javax.swing.JButton();
        EditLabel = new javax.swing.JLabel();
        Back = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));

        ItemPanel.setBackground(new java.awt.Color(255, 255, 255));

        StoreLabel.setFont(new java.awt.Font("Tahoma", 0, 24));
        StoreLabel.setText("Store Name");

        PostalLabel.setFont(new java.awt.Font("Tahoma", 0, 24));
        PostalLabel.setText("Postal Code");

        StNumberLabel.setFont(new java.awt.Font("Tahoma", 0, 24));
        StNumberLabel.setText("Street Number");

        ProvinceLabel.setFont(new java.awt.Font("Tahoma", 0, 24));
        ProvinceLabel.setText("Province");

        StreetLabel.setFont(new java.awt.Font("Tahoma", 0, 24));
        StreetLabel.setText("Street");

        PhoneLabel.setFont(new java.awt.Font("Tahoma", 0, 24));
        PhoneLabel.setText("Phone Number");

        CityLabel.setFont(new java.awt.Font("Tahoma", 0, 24));
        CityLabel.setText("City");

        CityFIeld.setFont(new java.awt.Font("Tahoma", 0, 24));

        StreetField.setFont(new java.awt.Font("Tahoma", 0, 24));

        StNumberField.setFont(new java.awt.Font("Tahoma", 0, 24));

        StoreField.setEditable(false);
        StoreField.setFont(new java.awt.Font("Tahoma", 0, 24));

        PostalField.setFont(new java.awt.Font("Tahoma", 0, 24));

        PhoneField.setFont(new java.awt.Font("Tahoma", 0, 24));

        ProvieceField.setEditable(false);
        ProvieceField.setFont(new java.awt.Font("Tahoma", 0, 24));

        javax.swing.GroupLayout ItemPanelLayout = new javax.swing.GroupLayout(ItemPanel);
        ItemPanel.setLayout(ItemPanelLayout);
        ItemPanelLayout.setHorizontalGroup(
            ItemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ItemPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(ItemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(PhoneLabel)
                    .addComponent(ProvinceLabel)
                    .addComponent(PostalLabel)
                    .addComponent(CityLabel)
                    .addComponent(StreetLabel)
                    .addComponent(StNumberLabel)
                    .addComponent(StoreLabel))
                .addGap(18, 18, 18)
                .addGroup(ItemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(ProvieceField, javax.swing.GroupLayout.DEFAULT_SIZE, 341, Short.MAX_VALUE)
                    .addComponent(CityFIeld, javax.swing.GroupLayout.DEFAULT_SIZE, 341, Short.MAX_VALUE)
                    .addComponent(StreetField, javax.swing.GroupLayout.DEFAULT_SIZE, 341, Short.MAX_VALUE)
                    .addComponent(StNumberField, javax.swing.GroupLayout.DEFAULT_SIZE, 341, Short.MAX_VALUE)
                    .addComponent(StoreField, javax.swing.GroupLayout.DEFAULT_SIZE, 341, Short.MAX_VALUE)
                    .addGroup(ItemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(PhoneField, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(PostalField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 341, Short.MAX_VALUE)))
                .addContainerGap())
        );
        ItemPanelLayout.setVerticalGroup(
            ItemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ItemPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(ItemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(StoreLabel)
                    .addComponent(StoreField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(ItemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(StNumberLabel)
                    .addComponent(StNumberField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(ItemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(StreetLabel)
                    .addComponent(StreetField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(ItemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CityLabel)
                    .addComponent(CityFIeld, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(ItemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PostalLabel)
                    .addComponent(PostalField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(ItemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(ItemPanelLayout.createSequentialGroup()
                        .addComponent(ProvinceLabel)
                        .addGap(26, 26, 26))
                    .addGroup(ItemPanelLayout.createSequentialGroup()
                        .addComponent(ProvieceField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)))
                .addGroup(ItemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(PhoneLabel)
                    .addComponent(PhoneField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(25, Short.MAX_VALUE))
        );

        ButtonPanel.setBackground(new java.awt.Color(255, 255, 255));

        ResetButton.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        ResetButton.setText("Reset");
        ResetButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ResetButtonActionPerformed(evt);
            }
        });

        SubmitButton.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        SubmitButton.setText("Submit");
        SubmitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SubmitButtonActionPerformed(evt);
            }
        });

        EditLabel.setFont(new java.awt.Font("Copperplate Gothic Bold", 2, 48)); // NOI18N
        EditLabel.setText("Edit Store");

        Back.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        Back.setText("<Back");
        Back.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BackActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout ButtonPanelLayout = new javax.swing.GroupLayout(ButtonPanel);
        ButtonPanel.setLayout(ButtonPanelLayout);
        ButtonPanelLayout.setHorizontalGroup(
            ButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ButtonPanelLayout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(EditLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 295, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31)
                .addComponent(ResetButton, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addComponent(SubmitButton, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addComponent(Back, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(55, Short.MAX_VALUE))
        );
        ButtonPanelLayout.setVerticalGroup(
            ButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, ButtonPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(ButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(EditLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 48, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, ButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(SubmitButton, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(ResetButton, javax.swing.GroupLayout.DEFAULT_SIZE, 48, Short.MAX_VALUE))
                    .addComponent(Back, javax.swing.GroupLayout.DEFAULT_SIZE, 48, Short.MAX_VALUE))
                .addGap(18, 18, 18))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(ButtonPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(115, 115, 115)
                        .addComponent(ItemPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(131, Short.MAX_VALUE)
                .addComponent(ItemPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(88, 88, 88)
                .addComponent(ButtonPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void SubmitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SubmitButtonActionPerformed
        // TODO add your handling code here:
        String city = CityFIeld.getText();
        String phoneNumber = PhoneField.getText();
        String postcode = PostalField.getText();
        String StNumber = StNumberField.getText();
        String streetName = StreetField.getText();
        String province = ProvieceField.getText();
        String storeName = StoreField.getText();
        Validation validation = new Validation();
        if (city.equals("") || streetName.equals("") || phoneNumber.equals("") ||
                postcode.equals("") || StNumber.equals("")) {
            JOptionPane.showMessageDialog(null, "Please input all information",
                    "Error", JOptionPane.ERROR_MESSAGE);
        } else if (!validation.ValidatePostal(postcode, province)) {
            JOptionPane.showMessageDialog(null, "Invalid Postal Code",
                    "Error", JOptionPane.ERROR_MESSAGE);
        } else {
            if (isNumeric(StNumber) && isNumeric(phoneNumber)) {
                Address address = new Address();
                address.setAptNo("1234");
                address.setCity(city);
                address.setID(aid);
                address.setProvince(province);
                address.setPostCode(postcode);
                address.setStNo(Integer.parseInt(StNumber));
                address.setStreet(streetName);
                Store store = new Store();
                store.setAddress(address);
                store.setPhone(phoneNumber);
                store.setStoreName(storeName);
                storeController.updateStore(store);
            } else {
                JOptionPane.showMessageDialog(null,
                        "Please input numbers for 'Street Number' and 'Phone Number'!",
                        "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_SubmitButtonActionPerformed

    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }

    private void ResetButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ResetButtonActionPerformed
        // TODO add your handling code here:
        StNumberField.setText((String) edit_vect.get(1).toString());
        StreetField.setText((String) edit_vect.get(2));
        CityFIeld.setText((String) edit_vect.get(3));
        PostalField.setText((String) edit_vect.get(4));
        ProvieceField.setText((String) edit_vect.get(5));
        PhoneField.setText((String) edit_vect.get(6));
    }//GEN-LAST:event_ResetButtonActionPerformed

    private void BackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BackActionPerformed
        storeController.goBackSearchStore();
    }//GEN-LAST:event_BackActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Back;
    private javax.swing.JPanel ButtonPanel;
    private javax.swing.JTextField CityFIeld;
    private javax.swing.JLabel CityLabel;
    private javax.swing.JLabel EditLabel;
    private javax.swing.JPanel ItemPanel;
    private javax.swing.JTextField PhoneField;
    private javax.swing.JLabel PhoneLabel;
    private javax.swing.JTextField PostalField;
    private javax.swing.JLabel PostalLabel;
    private javax.swing.JTextField ProvieceField;
    private javax.swing.JLabel ProvinceLabel;
    private javax.swing.JButton ResetButton;
    private javax.swing.JTextField StNumberField;
    private javax.swing.JLabel StNumberLabel;
    private javax.swing.JTextField StoreField;
    private javax.swing.JLabel StoreLabel;
    private javax.swing.JTextField StreetField;
    private javax.swing.JLabel StreetLabel;
    private javax.swing.JButton SubmitButton;
    // End of variables declaration//GEN-END:variables
}
